﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="org.aspx.cs" Inherits="SampleSite.admin.org" %>

<div>
    <table id="orgGrid" title="组织架构" iconcls="icon-edit"> 
    </table>
</div>

<%-- 添加公司 --%>
<div id="dlgAddOrg" class="easyui-dialog" style="width:330px;height:180px;padding:10px 20px"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="AddOrg" method="post" novalidate="novalidate">
        <table>
            <tr>
                <th colspan="2" >添加公司</th>
            </tr>     
            <tr>
                <td><label for="AddO_CName">公司中文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="O_CName" name="O_CName" 
                            style="width:175px;"
                            data-options="required:true,validType:['chinese']" />
                </td>
            </tr>
            <tr>
                <td><label for="AddO_EName">公司英文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="O_EName" name="O_EName" 
                            style="width:175px;"
                            data-options="validType:'english'" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center; padding-top:10px">
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton" 
                        id="btnAddOrg" 
                        iconcls="icon-ok" >确定</a>
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgAddOrg"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<%-- 添加部门 --%>
<div id="dlgAddDep" class="easyui-dialog" style="width:330px;height:200px;padding:10px 20px"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="AddDep" method="post" novalidate="novalidate">
        <table>
            <tr>
                <th colspan="2" >添加部门</th>
            </tr>     
            <tr>
                <td><label for="DepO_CName">部门中文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="DepO_CName" name="O_CName" 
                            style="width:175px;"
                            data-options="required:true,validType:['chinese']" />
                    <input type="hidden" id="DepO_ParentID" name="O_ParentID" />
                </td>
            </tr>
            <tr>
                <td><label for="O_EName">部门英文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="DepO_EName" name="O_EName" 
                            style="width:175px;"
                            data-options="validType:'english'" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center; padding-top:10px">
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton" 
                        id="btnAddDep" 
                        iconcls="icon-ok" >确定</a>
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgAddDep"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<%-- 修改 --%>
<div id="dlgUpDep" class="easyui-dialog" style="width:330px;height:200px;padding:10px 20px"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="UpDep" method="post" novalidate="novalidate">
        <table>
            <tr>
                <th colspan="2" >修改</th>
            </tr>     
            <tr>
                <td><label for="UpO_CName">中文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="UpO_CName" name="O_CName" 
                            style="width:175px;"
                            data-options="required:true,validType:['chinese']" />
                    <input type="hidden" id="UpO_ID" name="O_ID" />
                </td>
            </tr>
            <tr>
                <td><label for="UpO_EName">英文名：</label></td>
                <td><input class="easyui-validatebox" type="text" id="UpO_EName" name="O_EName" 
                            style="width:175px;"
                            data-options="validType:'english'" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center; padding-top:10px">
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton" 
                        id="btnUpDep" 
                        iconcls="icon-ok" >确定</a>
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgUpDep"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<script type="text/javascript">

    $(function () {
        //当页面首次刷新的时候执行的事件 
        initOrgGrid();
    });

    var dlgAddOrg = {
        clearAddOrgDialog: function () {
            $('#AddO_CName').val('');
            $('#AddO_EName').val('');
        },

        showAddOrgDialog: function () {
            dlgAddOrg.clearAddOrgDialog();
            $('#dlgAddOrg').dialog('open');
        },

        addOrg: function () {
            var validate = $('#AddOrg').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/addOrg.ashx', $('#AddOrg').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '添加成功');

                    $('#dlgAddOrg').dialog('close');
                    $('#orgGrid').treegrid('reload');
                }
                else {
                    $.messager.alert('提示', '添加失败:' + data.msg);
                }
            }, 'json');
        },

        init: function () {
            $('#btnAddOrg').click(dlgAddOrg.addOrg);
            $('#dlgAddOrg').dialog({ title: '添加公司' });
        }
    };

    var dlgAddDep = {
        clearAddDepDialog: function () {
            $('#DepO_CName').val('');
            $('#DepO_EName').val('');
            $('#DepO_ParentID').val('');
        },

        AddDepInit: function () {
            var rows = $('#orgGrid').treegrid('getSelections');
            $('#DepO_ParentID').val(rows[0].id);
        },

        showAddDepDialog: function () {
            dlgAddDep.clearAddDepDialog();
            dlgAddDep.AddDepInit();
            $('#dlgAddDep').dialog('open');
        },

        addDep: function () {
            var validate = $('#AddDep').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/addDep.ashx', $('#AddDep').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '添加成功');

                    $('#dlgAddDep').dialog('close');
                    $('#orgGrid').treegrid('reload');
                }
                else {
                    $.messager.alert('提示', '添加失败:' + data.msg);
                }
            }, 'json');
        },

        init: function () {
            $('#btnAddDep').click(dlgAddDep.addDep);
            $('#dlgAddDep').dialog({ title: '添加部门' });
        }
    };

    var dlgUpDep = {
        clearUpDepDialog: function () {
            $('#UpO_CName').val('');
            $('#UpO_EName').val('');
            $('#UpO_ID').val('');
        },

        UpDepInit: function () {
            var rows = $('#orgGrid').treegrid('getSelections');
            $('#UpO_ID').val(rows[0].id);
            $('#UpO_CName').val(rows[0].name);
            $('#UpO_EName').val(rows[0].ename);
        },

        showUpDepDialog: function () {
            dlgUpDep.clearUpDepDialog();
            dlgUpDep.UpDepInit();
            $('#dlgUpDep').dialog('open');
        },

        upDep: function () {
            var validate = $('#UpDep').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/updateDep.ashx', $('#UpDep').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '修改成功');

                    $('#dlgUpDep').dialog('close');
                    $('#orgGrid').treegrid('reload');
                }
                else {
                    $.messager.alert('提示', '修改失败:' + data.msg);
                }
            }, 'json');
        },

        init: function () {
            $('#btnUpDep').click(dlgUpDep.upDep);
            $('#dlgUpDep').dialog({ title: '修改' });
        }
    };

    function onDelete() {
        var row = $('#orgGrid').treegrid('getSelected');
        if (row) {
            $.messager.confirm(
                "提示", "是否删除当前选择项?",
                function (r) {
                    if (r == true) {
                        $.post(
                            '/admin/ajax/deleteOrg.ashx',
                            {
                                Id: row.id
                            },
                            function (data) {
                                if (data.state == 'ok') {
                                    $.messager.alert('提示', '删除成功');
                                    $('#orgGrid').treegrid('reload');
                                }
                                else
                                    $.messager.alert('提示', '删除失败');
                            }, 'json');
                    }
                });
        }
        else {
            $.messager.alert('提示', '请先选择需要删除的组织架构');
        }
    }

    function initOrgGrid() {
        $('#orgGrid').treegrid(
            {
                url: '/admin/ajax/getOrg.ashx',
                title: '组织架构',
                rownumbers: true,
                idField: 'id',
                treeField: 'name',
                columns: [[
                    { title: '中文名称', field: 'name', width: 240, sortable: false },
                    {
                        title: '英文名称',
                        field: 'ename',
                        width: 100,
                        sortable: false
                    }
                ]],
                toolbar: [{
                    id: '_btnAddOrg',
                    text: '添加公司',
                    iconCls: 'icon-add',
                    handler: function () {
                        dlgAddOrg.showAddOrgDialog();
                    }
                }, {
                    id: '_btnAddDep',
                    text: '添加部门',
                    iconCls: 'icon-add',
                    handler: function () {
                        var rows = $('#orgGrid').treegrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择所添加部门的上级！');
                        }
                        else {
                            dlgAddDep.showAddDepDialog();
                        }
                    }
                }, '-', {
                    id: 'btncut',
                    text: '修改',
                    iconCls: 'icon-cut',
                    handler: function () {
                        var rows = $('#orgGrid').treegrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的部门！');
                        }
                        else {
                            dlgUpDep.showUpDepDialog();
                        }
                    }
                }, '-', {
                    id: 'btnsave',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        onDelete();
                    }
                }]
            });

        // 绑定各个dialog的关闭事件
        $('.close_dlg').each(function (i, e) {
            var dlg = $(e).attr('dlg');
            $(e).click(function () {
                $('#' + dlg).dialog('close');
            });
        });

        dlgAddOrg.init();
        dlgAddDep.init();
        dlgUpDep.init();
    }
</script>
